﻿#include "charts/Inc/paintmap.h"
#include<QtMath>
#include<iostream>
paintMap::paintMap(QObject *parent) : QObject(parent)
{
    t1 = 0;
    t2 = 0;
    intv = 0.01;
    temp1 =0;
    temp2 = 0;
}


void paintMap::createCharts(Ui::QUICreator *UI)
{
    ui=UI;
    QChart *chart = new QChart();
    chart->setTitle("速度曲线");
    chart->setTheme(QChart::ChartTheme::ChartThemeLight);
    ui->chartView->setChart(chart);
    ui->chartView->setRenderHint(QPainter::Antialiasing);

    series0 = new QLineSeries();
    series1 = new QLineSeries();

    series0->setName("实际值");
    series1->setName("测量值");
    _curSeries = series0;

    QPen pen;
    pen.setStyle(Qt::SolidLine);
    pen.setWidth(2);
    pen.setColor(Qt::red);
    series0->setPen(pen);

    pen.setStyle(Qt::SolidLine);
    pen.setColor(Qt::blue);
    series1->setPen(pen);

    chart->addSeries(series0);
    chart->addSeries(series1);

    QValueAxis *axisX = new QValueAxis;
    _curAxis  =axisX;
    axisX->setLabelFormat("%.1f");
    //axisX->setTickCount(11);
    axisX->setRange(0,300);
    //axisX->setMinorTickCount(4);
    axisX->setTitleText("time(secs)");
    QValueAxis *axisY = new QValueAxis;
    axisY->setTitleText("value");
    //axisY->setTickCount(6);
    axisY->setRange(0,1);
    axisY->setLabelFormat("%.2f");
    //axisY->setMinorTickCount(4);

    chart->setAxisX(axisX,series0);
    chart->setAxisY(axisY,series0);
    chart->setAxisX(axisX,series1);
    chart->setAxisY(axisY,series1);
}
void paintMap::prepareData()
{
    series0->clear();
    series1->clear();
}
void paintMap::updateData0(double speed)
{
    t1++;
    temp1++;
    if(temp1 > 300)
    {
        real.pop_front();
    }
    real.push_back(QPointF(t1,speed));
    QVector<QPointF> newPoint;
    for(int i=0;i<real.size();i++)
    {
        QPointF point(i,real.at(i).y());
        newPoint.push_back(point);
    }
    series0->replace(newPoint);
    ui->chartView->update();
}
void paintMap::updateData1(double speed)
{
    t2++;
    temp2++;
    if(temp2 > 300)
    {
        fir.pop_front();
    }
    fir.push_back(QPointF(t2,speed));
    QVector<QPointF> newPoint;
    for(int i=0;i<fir.size();i++)
    {
        QPointF point(i,fir.at(i).y());
        newPoint.push_back(point);
    }
    series1->replace(newPoint);
    ui->chartView->update();
}
